﻿using System;
using System.Collections.Generic;
using HiTeachers.Data.Manager;
using HiTeachers.Entities;
using HiTeachers.Interfaces;
using HiTeachers.Services.Exceptions;

namespace HiTeachers.Services
{
    public class StudentFeedbackServices
    {
        //private static readonly IStudentFeedbackDao StdFeedbackDao = NHibernateDaoFactory.Instance.GetDao<IStudentFeedbackDao>("studentFeedback");
        private static readonly IStudentFeedbackDao StdFeedbackDao = NHibernateDaoFactory.Instance.GetStudentFeedbackDao();

        public static StudentFeedback GetById(Guid fbId)
        {
            return StdFeedbackDao.GetById(fbId, false);
        }

        public static StudentFeedback SaveOrUpdate(StudentFeedback studentFeedback)
        {
            if (!studentFeedback.ID.Equals(Guid.Empty))
                return StdFeedbackDao.SaveOrUpdate(studentFeedback);

            if (StdFeedbackDao.IsAlreadyFeedback(studentFeedback.Teacher.ID, studentFeedback.Email))
                throw new ObjectAlreadyExistException("You are already sent feedback for this teacher!");

            return StdFeedbackDao.Save(studentFeedback);
        }

        public static void Delete(Guid fbId)
        {
            StudentFeedback studentFeedback = StdFeedbackDao.GetById(fbId, false);
            StdFeedbackDao.Delete(studentFeedback);
        }

        public static void Delete(StudentFeedback studentFeedback)
        {
            StdFeedbackDao.Delete(studentFeedback);
        }

        public static int GetAvgRatingOnTeacher(Guid teacherId)
        {
            return StdFeedbackDao.GetAvgRatingOnTeacher(teacherId);
        }

        public static IList<StudentFeedback> GetFeedbacksByTeacher(Guid teacherId)
        {
            return StdFeedbackDao.GetFeedbacksByTeacher(teacherId);
        }

        #region Selection Methods
        public static int TotalNumberOfFeedback(int reviewStatus)
        {
            return Convert.ToInt32(StdFeedbackDao.TotalNumberOfFeedback(reviewStatus));
        }

        public static IList<StudentFeedback> GetFeedbacks(int reviewStatus)
        {
            return StdFeedbackDao.GetFeedbacks(reviewStatus);
        }

        public static IList<StudentFeedback> GetFeedbacks(int reviewStatus, int maximumRows, int startRowIndex)
        {
            return StdFeedbackDao.GetFeedbacks(reviewStatus, maximumRows, startRowIndex);
        }
        #endregion
    }
}
